package com.icangya.Utils;

import com.icangya.DAO.*;
import com.icangya.Value.FootBall;
import com.icangya.domain.*;
import com.icangya.service.IAdvertService;
import com.icangya.service.IMissionService;
import com.icangya.service.IMoneyService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Component
public class TaskUtil {

    org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    IAdvertService iAdvertService;

    @Autowired
    IAdvertRecordDAO iAdvertRecordDAO;

    @Autowired
    IAdvertClickDAO iAdvertClickDAO;

    @Autowired
    IGiftAdvertMoneyDAO iGiftAdvertMoneyDAO;

    @Autowired
    ICashRegisterDAO iCashRegisterDAO;

    @Autowired
    IRechargeRecordDAO iRechargeRecordDAO;

    @Autowired
    IGainRecordDAO iGainRecordDAO;

    @Autowired
    IFbUserMissionDAO iFbUserMissionDAO;

    @Autowired
    IMoneyService iMoneyService;

    @Autowired
    IMissionService iMissionService;

    @Autowired
    IUserFootDAO iUserFootDAO;
//俩点之后每三个小时执行一次
@Scheduled(cron = "0 0 2/3 * * ?")
public void run() {

    //将赠送用户的广告金超过一天的广告金回收
    List gitList = iGiftAdvertMoneyDAO.getAllGitAdvertMoney();
    if (gitList != null) {
        for (GiftAdvertMoney g : (List<GiftAdvertMoney>) gitList) {
            if (g.getGitAdvertMoney() > 0) {

                Date mDate = g.getModifiedTime();
                Date date = new Date();
                int timeDiff = (int) (date.getTime() - mDate.getTime()) / (1000 * 3600 * 24);
                if (timeDiff >= FootBall.ADVERT_OVER_DATE)//设置广告金过期时间，默认一天
                    iGiftAdvertMoneyDAO.updateGiftAdvertMoney(g.openId, -g.getGitAdvertMoney());
            }
        }

    }
    //执行判断广告信息的操作
    //首先查询所有的广告信息
    List list = iAdvertRecordDAO.getAdvertAll();
    Date date = new Date();
    for (AdvertRecord adv : (List<AdvertRecord>) list) {
        Date mDate = adv.getModifiedTime();
        int timeDiff = (int) (date.getTime() - mDate.getTime()) / (1000 * 3600 * 24);

        //该广告赠送的广告金为1   使用赠送广告金的时间超过一天
        if (adv.getGiftAdvertMoney() == 1) {
            if( timeDiff >= FootBall.ADVERT_OVER_DATE){
                //将该广告下线
                //修改该条广告的状态为 1，广告费不足
                iAdvertRecordDAO.updateAdvertByStatus(adv.getId());
                logger.info(">>>>>>>>>>>>>>>>>>>run............."+adv.getId());
            }
        }
    }

    logger.info(">>>>>>>>>>>>>>>>>>>run.............");
}

//    @Scheduled(cron = "0 0/1 * * * ?") //一分钟执行一次
    @Scheduled(cron = "0 55 23 * * ?")
    public void gain() {
        Timestamp timestamp=new Timestamp(new Date().getTime());
       double cashMoney= iCashRegisterDAO.getCashMoneyNow(timestamp);
       double rechargeMoney= iRechargeRecordDAO.getRechargeMoneyNow(timestamp);
       double gainMoney=rechargeMoney-cashMoney;
//        (double gainMoney, double expendMoney, double rechargeMoney

//        private  double gainMoney;  //实际收入
//        private  double expendMoney;//支出  584.2700000000001
//        private  double rechargeMoney;//充值  5010.060000000001

        GainRecord gainRecord=new GainRecord(gainMoney,cashMoney,rechargeMoney,timestamp);
        iMoneyService.saveGainRecord(gainRecord);
//        iGainRecordDAO.save(gainRecord);

        logger.info(">>>>>>>>>>>>>>>>>>>gain.............");
    }

//    @Scheduled(cron = "0 0/1 * * * ?") //一分钟执行一次
    @Scheduled(cron = "0 0 1 * * ?")
    public void mission() {

        List<FbUserMission> list=iFbUserMissionDAO.getFbUserMission();
            if(list.size()>0){
                Date date=new Date();
                for (FbUserMission userMission:list) {
                    int timeDiff = (int) (userMission.getCreateTime().getTime()-date.getTime() ) / (1000 * 3600 * 24);
                    logger.info("timDiff================="+timeDiff);
                    if(timeDiff<0) {
                        userMission.setStatus(2);//设置任务过期
                        iMissionService.updateUserMission(userMission);
                        logger.info(">>>>>>>>>>>>>>>>>>>missionid================" + userMission.getId());
                    }
                }
            }

        logger.info(">>>>>>>>>>>>>>>>>>>mission.............");
    }

//
//    @Scheduled(cron = "0 0 1 * * ?")
//    public void turntableUser() {
//            if(FootBall.MISSION_TURNTABLE==0){
//                //获取可领取的奖励
//                List<FbMissionAward> missionAwards = iMissionService.getMissionAwardStatus();
//                if(missionAwards!=null) {
//                    Random random = new Random();
//                    int result = random.nextInt(missionAwards.size());
//                    FbMissionAward missionAward = missionAwards.get(result);  //获取奖励信息
//
//                    FbTurntableRecord turntableRecord= iMissionService.getTurntableRecordBySystem(0,user.getOpenId());
//                    if(turntableRecord==null){
//                        //添加转盘记录
//                        //String nickName, String openId, long uid, String headImgUrl, long missionId, String missionContent, long awardId, String awardContent, int status
//                        FbTurntableRecord fbTurntableRecord=new FbTurntableRecord(user.getNickName(),user.getOpenId(),user.getId(),user.getHeadImgUrl(),0,
//                                "赠送体验",missionAward.getId(),missionAward.getAwardContent(),0,new Timestamp(new Date().getTime()));
//                        iMissionService.saveFbTurntableRecord(fbTurntableRecord);
//                    }
//
//                }
//            }
//
//        logger.info(">>>>>>>>>>>>>>>>>>>turntableUser.............");
//    }














}
